package cn.shyouchi.wuguanjia.android.ui.event.temporary;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventTemporaryActivity extends BaseActivity implements TemporaryContract.View {

    public final static int REQUEST_TYPE_ADD = 10001;
    public final static int REQUEST_TYPE_ADD_SUCCESS = 10002;

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_temp)
    RecyclerView mRvTemp;
    @BindView(R.id.btn_add_temp)
    Button mBtnAddTemp;

    TemporaryAdapter mAdapter;

    String projectId;
    boolean needRefresh = true;


    List<TemporaryEntity.ResultMessageBean> mResultMessageBeanList = new ArrayList<>();

    EventTemporaryPresenter mTemporaryPresenter;

    @Override
    public int initContentView() {
        return R.layout.activity_temporary;
    }

    @Override
    public void initInjector() {
        projectId = getIntent().getStringExtra("projectId");
        mTemporaryPresenter = new EventTemporaryPresenter(mContext, this);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mAdapter = new TemporaryAdapter(mResultMessageBeanList, this);
        mRvTemp.setLayoutManager(new LinearLayoutManager(mContext));
        mRvTemp.setAdapter(mAdapter);
        EventBus.getDefault().register(this);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.btn_add_temp)
    public void onClick() {
        Intent intent = new Intent(mContext, AddTemporaryActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD && resultCode == REQUEST_TYPE_ADD_SUCCESS) {
            mTemporaryPresenter.getTemporaryList(projectId);
        }
    }

    @Override
    public void onTemporaryRefresh(List<TemporaryEntity.ResultMessageBean> resultMessageBeanList) {
        this.mResultMessageBeanList.clear();
        this.mResultMessageBeanList.addAll(resultMessageBeanList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(TemporaryEntity.ResultMessageBean resultMessageBean) {
        LogUtil.json(resultMessageBean);
        Intent intent = new Intent(mContext, PreviewTemporaryActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("temporary", resultMessageBean);
        startActivity(intent);
    }

    @Override
    public void onDelClick(TemporaryEntity.ResultMessageBean resultMessageBean) {
        mTemporaryPresenter.delTemporary(projectId, String.valueOf(resultMessageBean.getId()));

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mTemporaryPresenter.getTemporaryList(projectId);
            needRefresh = false;
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }


}
